package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * User: mini server
 * Date: 9/20/13
 * Time: 11:22 AM
 * To change this template use File | Settings | File Templates.
 */
public class BookInventory implements Inventory
{
    private ArrayList<Book> books;

    public BookInventory()
    {
        books = new ArrayList<Book>();
        books.add(new Book("LOTRO", "ONLINE_GAME_001",2));
        books.add(new Book("Champu Times", "CHILDREN_BOOK_01", 24));
    }

    public BookInventory(ArrayList<Book> books)
    {
        this.books = books;
    }

    @Override
    public int numOfItemsInInventory() {
        return books.size();
    }

    @Override
    public int typeOfInventory() {
        return Inventory.BOOK_INVENTORY;
    }

    @Override
    public Iterator returnIterator() {
        return books.iterator();
    }

    @Override
    public String list() {
        String result = "";
        Iterator iteratorOverBooks = books.iterator();

        while(iteratorOverBooks.hasNext())
        {
            Book book = (Book)iteratorOverBooks.next();
            result = result + book.display() + "\n";
        }

        return result;

    }

    @Override
    public boolean reserveAnItem(String id) {
        Iterator iteratorOverBooks = books.iterator();
        while (iteratorOverBooks.hasNext())
        {
            Book book = (Book)iteratorOverBooks.next();
            if (book.match(id))
            {
                return book.reserve();
            }
        }

        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
